package com.puxiang.mall.module.shop.viewModel;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.ObservableField;
import android.text.TextUtils;

import com.puxiang.mall.BR;
import com.puxiang.mall.MyApplication;
import com.puxiang.mall.config.CacheKey;
import com.puxiang.mall.model.data.RxArea;
import com.puxiang.mall.module.shop.adapter.HotCityAdapter;
import com.puxiang.mall.module.shop.view.SelectCityActivity;
import com.puxiang.mall.mvvm.base.ViewModel;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaoyong bai on 2017/10/25.
 */

public class CityHeadViewModel extends BaseObservable implements ViewModel {
    private SelectCityActivity activity;
    private HotCityAdapter latestAdapter;
    private HotCityAdapter hotAdapter;
    private ObservableField<String> currentCity = new ObservableField<>("");
    private List<RxArea> hotList = new ArrayList<>();
    private List<RxArea> latestList = new ArrayList<>();

    public CityHeadViewModel(SelectCityActivity activity, HotCityAdapter hotAdapter, HotCityAdapter latestAdapter) {
        this.activity = activity;
        this.latestAdapter = latestAdapter;
        this.hotAdapter = hotAdapter;
        String city = activity.getIntent().getStringExtra("city");
        setCurrentCity(city);
        setHotData();
        setLatestCity();
    }

    private void setLatestCity() {
        MyApplication.mCache.getAsListBean(CacheKey.LATESTCITY, RxArea[].class, rxCities -> {
            if (rxCities != null) {
                latestList.addAll(rxCities);
                latestAdapter.setNewData(latestList);
            }
        });

        latestAdapter.setOnItemClickListener((adapter, view, position) -> {
            setCurrentCity(latestList.get(position).getName());
            postCity(latestList.get(position));
            activity.onBackPressed();
        });
    }

    private void postCity(RxArea rxCity) {
        EventBus.getDefault().post(rxCity);
    }

    private void setHotData() {
        RxArea rxCity5 = new RxArea();
        rxCity5.setAreaName("郑州市");
        rxCity5.setAreaCode("410100");
        rxCity5.setIsVisiable(false);
        hotList.add(rxCity5);

        RxArea rxCity = new RxArea();
        rxCity.setAreaName("南阳市");
        rxCity.setAreaCode("411300");
        rxCity.setIsVisiable(false);
        hotList.add(rxCity);

        RxArea rxCity1 = new RxArea();
        rxCity1.setAreaName("信阳市");
        rxCity1.setAreaCode("411500");
        rxCity1.setIsVisiable(false);
        hotList.add(rxCity1);

        RxArea rxCity3 = new RxArea();
        rxCity3.setAreaName("新乡市");
        rxCity3.setAreaCode("410700");
        rxCity3.setIsVisiable(false);
        hotList.add(rxCity3);

        RxArea rxCity2 = new RxArea();
        rxCity2.setAreaName("许昌市");
        rxCity2.setAreaCode("411000");
        rxCity2.setIsVisiable(false);
        hotList.add(rxCity2);

        RxArea rxCity4 = new RxArea();
        rxCity4.setAreaName("焦作市");
        rxCity4.setAreaCode("410800");
        rxCity4.setIsVisiable(false);
        hotList.add(rxCity4);

        hotAdapter.setNewData(hotList);
        hotAdapter.setOnItemClickListener((adapter, view, position) -> {
            setCurrentCity(hotList.get(position).getName());
            dealLatestData(hotList.get(position));
            postCity(hotList.get(position));
            activity.onBackPressed();
        });
    }

    private void dealLatestData(RxArea rxCity) {
        rxCity.setIsVisiable(true);
        if (latestList.size() > 0) {
            for (int i = 0; i < latestList.size(); i++) {
                latestList.get(i).setIsVisiable(false);
                if (TextUtils.equals(rxCity.getName(), latestList.get(i).getName())) {
                    latestList.remove(i);
                    break;
                }
            }
        }

        latestList.add(0, rxCity);

        if (latestList.size() > 3) {
            latestList = latestList.subList(0, 3);

        }
        MyApplication.mCache.put(CacheKey.LATESTCITY, latestList);
        latestAdapter.notifyDataSetChanged();

    }

    public void setCurrentCity(String city) {
        this.currentCity.set(city);
        notifyPropertyChanged(BR.currentCity);
    }

    @Bindable
    public String getCurrentCity() {
        return currentCity.get();
    }

    @Override
    public void destroy() {

    }
}
